package com.atguigu.order.controller;


import com.atguigu.commonutils.Result;
import com.atguigu.order.service.TPayLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.concurrent.TimeUnit;
import java.util.Map;

/**
 * <p>
 * 支付日志表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2024-07-10
 */
@RestController
@RequestMapping("/order/payLog")
@CrossOrigin
@Slf4j
public class TPayLogController {
    @Autowired
    private TPayLogService payLogService;

    //生产微信支付二维码接口
    //参数是订单号
    @GetMapping("createPayLog/{orderNo}")
    public Result createPayLog(@PathVariable String orderNo) {
        Map map = payLogService.createPayLog(orderNo);
        log.info("返回二维码map:{}",map);
        return Result.ok().data("map",map);
    }

    //查询订单支付状态
    //
    @GetMapping("queryPayStatus/{orderNo}")
    public Result queryPayStatus(@PathVariable String orderNo) {
        Map<String,String> map = payLogService.queryPayStatus(orderNo);
        try {
            TimeUnit.SECONDS.sleep(30);
        } catch (InterruptedException e) {
          e.printStackTrace();
        }
        if(map == null){
            log.info("查询订单状态的map:{}",map);
            return Result.error().message("支付出错了");
        }
        //通过map获取订单状态
        if ("SUCCESS".equals(map.get("trade_state"))){//支付成功
            //添加记录到支付宝，更新订单表订单的状态
            payLogService.updateOrdersStatus(map);
            return Result.ok();
        }
        return Result.ok().code(25000).message("支付中");
    }
}

